package model

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

// RedisPool 创建全局redis连接池句柄
var RedisPool redis.Pool

func InitRedis() {
	// 链接Redis
	RedisPool = redis.Pool{
		MaxIdle:         20,
		MaxActive:       50,
		MaxConnLifetime: 60 * 5,
		IdleTimeout:     60,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", "127.0.0.1:6379")
		},
	}
}

// CheckImgCode 检验图片验证码
func CheckImgCode(uuid, imgCode string) bool {
	//conn, err := redis.Dial("tcp", "127.0.0.1:6379")
	//if err != nil {
	//	fmt.Println("redis.Dial err:", err)
	//	return false
	//}
	conn := RedisPool.Get()
	defer conn.Close()

	// 查询redis数据
	code, err := redis.String(conn.Do("get", uuid))
	if err != nil {
		fmt.Println("查询错误err: ", err)
		return false
	}

	return code == imgCode
}

func SaveSmsCode(phone, code string) error {
	// 从连接池中获取一个连接
	conn := RedisPool.Get()
	defer conn.Close()

	// 存储短信验证码到redis中
	_, err := conn.Do("setex", phone+"_code", 60*3, code)

	return err
}

func Login(mobie, pwd string) {

}
